package com.lzcu.maoyan.mapreduce.connectionPool;


import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * phoenix 连接池
 *
 * @author WangLiang
 * @date `2023/11/22`
 */

public class ConnectionPhoenix {
    private static ConnectionPhoenix instance;
    private static final Logger logger = LoggerFactory.getLogger(ConnectionPhoenix.class);
    private final HikariDataSource dataSource;

    private ConnectionPhoenix() {
        String url = "jdbc:phoenix:192.168.2.109,192.168.2.110,192.168.2.112";
        this.dataSource = new HikariDataSource();
        this.dataSource.setJdbcUrl(url);
        // 设置最大连接数
        this.dataSource.setMaximumPoolSize(10000);
        // 设置连接超时时间（单位：毫秒）
        this.dataSource.setConnectionTimeout(30000);
        this.dataSource.addDataSourceProperty("phoenix.schema.isNamespaceMappingEnabled", "true");
    }

    public static ConnectionPhoenix getInstance() {
        if (instance == null) {
            instance = new ConnectionPhoenix();
            logger.info("创建phoenix连接池");
        }
        return instance;
    }

    public Connection getConnection() throws SQLException {
        return getInstance().dataSource.getConnection();
    }

}


